<?php

namespace App\Http\Controllers;

use App\Models\Vote;
use App\Models\VoteTags;
use Illuminate\Http\Request;
use Illuminate\Support\Env;
use Illuminate\Database\Eloquent\Builder;

class VoteController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @param $classify
     * @param $tags
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function index($classify = null,$tags = null)
    {

        $result['result'] = Vote::where('is_using',Env::get('APP_CONTENT_STATE'))
            ->when($classify,function(Builder $query,string $classify) {
                if(!empty($classify)) {
                    $query->where('c_key',$classify);
                }
            })
            ->when($tags,function(Builder $query,string $tags) {
                if(!empty($tags)) {
                    $query->where('t_key',$tags);
                }
            })
            ->orderBy('sort_id','desc')
            ->orderBy('id','desc')
            ->paginate(getConf('POST_VIEW_NUM'));

        return view(config('template') . '.vote.index',['result' => $result]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param $slug
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Foundation\Application
     */
    public function show($slug)
    {

        $result['result'] = Vote::where('is_using',Env::get('APP_CONTENT_STATE'))
            ->where('slug',$slug)
            ->with('admin_users')
            ->with('vote_cls')
            ->first();

        if(empty($result['result']->title)) {
            abort(404);
        }

        $images = json_decode($result['result']->images);

        // 标签
        if(!empty($result['result']->t_key)) {

            $tagsArray = json_decode($result['result']->t_key);

            foreach($tagsArray as $key => $value) {

                if(empty($value)) {
                    continue;
                }

                $postTags = VoteTags::where('t_key',$value)->where('is_using',Env::get('APP_CONTENT_STATE'))->first();

                if(empty($postTags->name)) {
                    continue;
                }

                $result['tags'][$key]['name'] = $postTags->name;
            }
        }
        else {
            $result['tags'] = [];
        }

        // 关键字
        if(!empty($result['result']->keywords)) {

            $keyArray = explode(',',$result['result']->keywords);

            foreach($keyArray as $value) {

                if(empty($value))
                    continue;

                $result['keywords'][] = $value;
            }
        }

        return view(config('template') . '.vote.show',['result' => $result,'images' => $images]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int                      $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
